<?php


	namespace Admin\Controller;

	use \Think\Controller;

	class PropertyController extends Controller{
		public function index(){
			$catModel = D('Category');
			$list = $catModel ->field(true)->select();
			$arr = $catModel->getSubTree($list,0);
			$this -> assign('arr',$arr);
			$this -> display();
		}

	
		public function ajaxShowPro(){
			$arr = [];
			$cat_proModel = M('cat_pro');
			$proModel = M('Property');
			if(IS_AJAX){

				//遍历分类属性表
				
				if($data = $cat_proModel -> where(['catId'=>I('post.cid')]) -> select()){
				
					foreach($data as $key => $val){ 
						$proArr= $proModel ->field('pro.name proname,proval.name value,pro.id proid,proval.id valid')->alias('pro') -> join('LEFT JOIN __PROVAL__ proval on proval.proid = pro.id') -> where('pro.id='.$val['proid'])->select();
						foreach($proArr as $k => $value){
							$arr[$value['proname']][]=$value;
						}
					}
					// dump($arr);
					$this -> assign('arr',$arr);
					$this -> display('Property/showproperty');
				}else{
					// dump($arr);
					$this -> assign('arr',$arr);
					$this -> display('Property/showproperty');
				}
			}
		}

		public function addIndex(){
			$catModel = D('Category');
			$list = $catModel ->field(true)->select();
			$arr = $catModel->getSubTree($list,0);
			$this -> assign('arr',$arr);
			$this -> display();
		}

		public function add(){
			$proModel = D('Property');
			if(empty($_POST['val'])){
				$this -> error('属性值不能为空!',U('Property/index'));
			}
			$info = $proModel -> addPropery(I('post.cid'),I('post.property'),I('post.val'));
			
			if($info['flag']){
				$this -> success('成功插入属性!',U('Property/index'));
			}else{
				$this -> error($info['error'],U('Property/index'));
			}
		}

		public function checkPro(){
			$proModel = D('Property');
			$info = $proModel->checkcatPro(I('post.cid'),I('post.proname'));
			$this -> ajaxReturn($info) ;
		}

		public function delPro(){
			if(IS_AJAX){
				$proModel = D('Property');
				if($proModel -> delPro(I('post.proid'))){
					$this -> ajaxReturn(['code'=>0]) ;

				}else{

					//失败
					$this -> ajaxReturn(['code'=>1]) ;
				}
			}else{
				$this -> error('非法操作',U('Property/index'));
			}	
		}

		public function addVal(){
			if(IS_AJAX){
				foreach($_POST['val'] as $value){
						$arr[] = ['proId'=>$_POST['proid'],'name'=>$value['value']];
				}
					//成功
				if(M('proval')->addAll($arr)){
					$this -> ajaxReturn(['code'=>0]) ;
				}else{
					$this -> ajaxReturn(['code'=>1]) ;
				}
			}else{
				$this -> error('非法操作',U('Property/index')); 
			}
		}
		// public function getHTML($data){
		// 	// $html = '';
		// 	// foreach($data as $key => $value){
		// 	// 	$html.= '<tr>
		// 	// 			<td>'.$key.'</td><td>';
		// 	// 			foreach($value as $k => $val){
		// 	// 			    $val['value'].' ';
		// 	// 			}
						
		// 	// 	echo		'</td><td>
		// 	// 				<a href=""><button type="button" class="btn btn-danger">删除</button></a>
		// 	// 				<a href=""><button type="button" class="btn btn-success">编辑</button></a>
		// 	// 			</td>
		// 	// 	    </tr>';
		// 	// }
		// }
		// 
		

		/**
		 * [getProByid  通过ajax传过来属性id获得所有的属性值]
		 * @return [type] [description]
		 */
		public function getProByid(){

			//判断是不是ajax
			if(IS_AJAX){
				
				$proid = $_POST['proid'];
				$data = M('property')->alias('pro')
							 -> field('pro.name proname,pro.id proid,val.name valname,val.id valid')
							 ->join('__PROVAL__ val ON val.proId= pro.id')
							 ->where(['pro.id'=>$proid])
							 ->select();

				$this -> assign('valArr',$data );
				$this -> display('editProperty');
			}else{
				$this -> error('非法操作',U('Property/index'));
			}
		}

		/**
		 * [doeditPro 编辑属性的函数  支持批量修改]
		 * @return [type] [description]
		 */
		public function doeditPro(){

			$proval = M('proval');
			$property = M('property');

			$data = $_POST;
			$proid = $data['proid'];
			$addArr = [];
			foreach($data['val'] as $key => $value){
				$addArr[] = ['proId'=>$proid,'name'=>$value,'id'=>$key];
			}	


			// var_dump($addArr);
			// dump($proid);
			// dump($data['proname']);

			//更新属性表
			if($row = $property -> add(['id'=>$proid,'name'=>$data['proname']],'',['name'])>=0){
				if($rows = $proval -> addAll($addArr,'',['proId','name'])>=0){
					$this -> ajaxReturn(['code'=>0,'mess'=>'更新属性成功!']);
				}else{
					$this -> ajaxReturn(['code'=>1,'mess'=>'更新属性值失败！请稍后再试','row'=>$rows]);
				}
			}else{
				$this -> ajaxReturn(['code'=>1,'name'=>$data['proname'],'mess'=>'更新属性名失败！请稍后再试','row'=>$row]);
			}

			//更新属性值表
			
			//先修改属性值表的内容		
		}

		/**
		 * [delVal 删除属性值得函数]
		 * @return [type] [description]
		 */
		public function delVal(){
			if(IS_AJAX){
				if(M('proval')->delete($_POST['valid'])){
					$this -> ajaxReturn(['code'=>0,'mess'=>'删除成功!']);
				}else{
					$this->ajaxReturn(['code'=>1,'mess'=>'删除失败!']);
				}
			}
		}


	}